home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / Lex.cpt / Lex / LEXLIB.π folder / MAC_YYLEX.C < prev    next >
Text File  |  1990-06-04  |  5KB  |  220 lines

  1. /*
  2.   HEADER: CUG    nnn.nn;
  3.   TITLE:    LEX - A Lexical Analyser Generator
  4.   VERSION:       1.0 for IBM-PC
  5.   DATE:    Jan 30, 1985
  6.   DESCRIPTION:   A Lexical Analyser Generator. From UNIX
  7.   KEYWORDS:      Lexical Analyser Generator YACC C PREP
  8.   SYSTEM:    IBM-PC and Compatiables
  9.   FILENAME:      YYLEX.C
  10.   WARNINGS:      This program is not for the casual user. It will
  11.     be useful primarily to expert developers.
  12.   CRC:    N/A
  13.   SEE-ALSO:      YACC and PREP
  14.   AUTHORS:       Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     DESMET-C
  16.   REFERENCES:    UNIX Systems Manuals
  17. */
  18. /*
  19.  * Bob Denny 28-Aug-82  Remove reference to stdio.h
  20.  * Remove code to default lexin, change to call to
  21.  * llstin(), generated by lex depending upon setting
  22.  * of "-s" switch.  Eliminates hardwired dependency
  23.  * on standard I/O library.  Moved declaration of
  24.  * lexin to lexgetc().
  25.  *
  26.  * Bob Denny 31-Aug-82  Add call to lexswitch() in
  27.  * the generated file, to switch to the table whose
  28.  * name was given in the "-t" switch (or to "lextab"
  29.  * if "-t" wasn't given).  Removed hardwired setting
  30.  * of _tabp --> "lextab" here. Now handled automagically.
  31.  *
  32.  * Bob Denny 21-Oct-82  Add llinit() function to re-initialize
  33.  * yylex(), making it serially reusable.
  34.  *
  35.  * Initialize _tabp to NULL so lexswitch() to real table happens
  36.  * only once.
  37.  *
  38.  * Bob Denny 15-Apr-83 Move NBPW to LEX.H and make it 32 on VAX native,
  39.  *    else 16.
  40.  * Scott Guthery 20-Nov-83      Adapt for IBM PC & DeSmet C
  41.  *
  42.  *    Maarten Meijer 20 Apr 1990 -- Port to THINK C on the Mac, added call
  43.  *        to yyinit() to allow reading yyparse() tables from resources
  44.  */
  45.  
  46. /*
  47.  * yylex for lex tables
  48.  */
  49. #ifdef  THINK_C
  50. #include <stdlib.h>
  51. #endif
  52.  
  53. #include "lex.h"
  54.  
  55. #define ERROR   256     /* yacc's value */
  56.  
  57. SHORTINT    tst__b(register SHORTINT c, unsigned char tab[]);
  58. SHORTINT    llinp(void);
  59. SHORTINT    llset(void);
  60. void        llinit(void);
  61. SHORTINT    yylex(void);
  62. void        llstin(void);
  63.  
  64. tst__b(c, tab)
  65. register SHORTINT    c;
  66. unsigned char    tab[];
  67. {
  68.     return(tab[(c >> 3) & 037] & (1 << (c & 07)) );
  69. }
  70.  
  71. struct  lextab  *_tabp = 0;
  72.  
  73. extern char     *llsave[];    /* Right-context look ahead buffer    */
  74. char            *llbuf = NULL;        /* work buffer    */
  75. char            *llp1 ;        /*= &llbuf[0];    /* pointer to next avail.in token */
  76. char            *llp2 ;        /*= &llbuf[0];    /* pointer to end of lookahead */
  77. char            *llend;        /*= &llbuf[0];    /* pointer to end of token */
  78. char            *llebuf;    /*= &llbuf[sizeof(llbuf)]; */
  79. SHORTINT        llbufsize = 0;
  80. SHORTINT        lleof;
  81. SHORTINT        yyline  = 0;
  82.  
  83. SHORTINT
  84. yylex()
  85. {
  86.     register SHORTINT c, llstate;
  87.     SHORTINT final, l, llk, i;
  88.  
  89.     register struct lextab *lextabp;
  90.     unsigned char *cp;
  91.  
  92.     /*
  93.     * Call llstin() to default lexin to stdin
  94.     * and assign _tabp to "real" table.
  95.     */
  96.     llstin();    /* Initialize yylex() variables */
  97. loop:
  98.     llk = 0;
  99.     if (llset())
  100.         return(0);    /* Prevent EOF loop     */
  101.     llstate = 0;
  102.     final = -1;
  103.     lextabp = _tabp;
  104.  
  105.     do {
  106.         if (lextabp->lllook && (l = lextabp->lllook[llstate]))
  107.             {
  108.             for (c = 0; c < NBPW; c++)
  109.                 if (l&(1<<c))
  110.                     llsave[c] = llp1;
  111.             llk++;
  112.             }
  113.         if ((i = lextabp->llfinal[llstate]) != -1)
  114.             {
  115.             final = i;
  116.             llend = llp1;
  117.             }
  118.         if ((c = llinp()) < 0)
  119.             break;
  120.  
  121.         if ((cp = lextabp->llbrk) && llk==0 && tst__b(c, cp)) /* break char */
  122.             {
  123.             llp1--;
  124.             break;
  125.             }
  126.     } while ((llstate = (*lextabp->llmove)(lextabp, c, llstate)) != -1);
  127.  
  128.  
  129.     if (llp2 < llp1)
  130.         llp2 = llp1;
  131.     if (final == -1) 
  132.         {
  133.         llend = llp1;
  134.         if (llstate == 0 && c < 0)
  135.             return(0);
  136.         if ((cp = lextabp->llill) && tst__b(c, cp)) 
  137.             {
  138.             lexerror("Illegal character: %c (%d)", c, c);
  139.             goto loop;
  140.             }
  141.         return(ERROR);
  142.         }
  143.     if (c = (final >> 11) & 037)
  144.         llend = llsave[c-1];
  145.     if ((c = (*lextabp->llactr)(final & 03777)) >= 0)
  146.         return(c);
  147.     goto loop;
  148. }
  149.  
  150. SHORTINT
  151. llinp()
  152. {
  153.  
  154.     register c;
  155.     register struct lextab *lextabp;
  156.     register unsigned char *cp;
  157.  
  158.     lextabp = _tabp;
  159.     cp = lextabp->llign;    /* Ignore class    */
  160.     for (;;) 
  161.         {
  162.         /*
  163.         * Get the next character from the save buffer (if possible)
  164.         * If the save buffer's empty, then return EOF or the next
  165.         * input character.  Ignore the character if it's in the
  166.         * ignore class.
  167.         */
  168.         c = (llp1 < llp2) ? *llp1 & 0377 : (lleof) ? EOF : lexgetc();
  169.         if(c == '\n')
  170.             yyline++;    /* added 90/06/03 MM */
  171.         if (c >= 0) 
  172.             {    /* Got a character?     */
  173.             if (cp && tst__b(c, cp))
  174.                 continue;       /* Ignore it    */
  175.             if (llp1 >= llebuf) 
  176.                 {   /* No, is there room?   */
  177.                 lexerror("Token buffer overflow");
  178.                 exit(1);
  179.                 }
  180.             *llp1 = c;    /* Store in token buff  */
  181.             llp1++;
  182.             }
  183.         else
  184.             lleof = 1;    /* Set EOF signal       */
  185.         return(c);
  186.         }
  187. }
  188.  
  189. SHORTINT
  190. llset()
  191. /*
  192.  * Return TRUE if EOF and nothing was moved in the look-ahead buffer
  193.  */
  194.  
  195. {
  196.     register char *lp1, *lp2;
  197.  
  198.     for (lp1 = llbuf, lp2 = llend; lp2 < llp2;) {
  199.         *lp1 = *lp2++;
  200.         lp1++;
  201.         }
  202.     llend = llp1 = llbuf;
  203.     llp2 = lp1;
  204.     return(lleof && lp1 == llbuf);
  205. }
  206.  
  207. /*
  208.  * Re-initialize yylex() so that it can be re-used on
  209.  * another file.
  210.  */
  211. void
  212. llinit()
  213.    {
  214.    extern    void yyinit(void);
  215.    llp1 = llp2 = llend = &llbuf[0];
  216.    llebuf = llbuf + llbufsize;
  217.    lleof = yyline = 0;
  218.    yyinit();    /* to check for resources */
  219.    }
  220.